namespace eCommerce.Data.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class init : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Addresses",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Email = c.String(),
                        Province = c.String(),
                        MobileNumber = c.String(),
                        PhoneNumber = c.String(),
                        ZipPostalCode = c.String(),
                        Address2 = c.String(),
                        Address1 = c.String(),
                        City = c.String(),
                        LastName = c.String(),
                        FirstName = c.String(),
                        CreatedOnUtc = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Blogs",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Brands",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 400),
                        Description = c.String(),
                        PictureId = c.Int(nullable: false),
                        Published = c.Boolean(nullable: false),
                        Deleted = c.Boolean(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                        CreatedOnUtc = c.DateTime(nullable: false),
                        UpdatedOnUtc = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Product_Brand_Mapping",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ProductId = c.Int(nullable: false),
                        BrandId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Brands", t => t.BrandId, cascadeDelete: true)
                .ForeignKey("dbo.Products", t => t.ProductId, cascadeDelete: true)
                .Index(t => t.ProductId)
                .Index(t => t.BrandId);
            
            CreateTable(
                "dbo.Products",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ShopId = c.Int(nullable: false),
                        ProductName = c.String(nullable: false, maxLength: 400),
                        ShortDescription = c.String(),
                        FullDescription = c.String(),
                        Published = c.Boolean(nullable: false),
                        Deleted = c.Boolean(nullable: false),
                        CreatedOn = c.DateTime(nullable: false),
                        UpdatedOn = c.DateTime(nullable: false),
                        ProductAttributeKeyId = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Shops", t => t.ShopId)
                .Index(t => t.ShopId);
            
            CreateTable(
                "dbo.ProductAttributeKeys",
                c => new
                    {
                        Id = c.Int(nullable: false),
                        ProductId = c.Int(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                        Name = c.String(),
                        Deleted = c.Boolean(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Products", t => t.Id)
                .Index(t => t.Id);
            
            CreateTable(
                "dbo.ProductAttributeValues",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ProductAttributeKeyId = c.Int(nullable: false),
                        Value = c.String(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ProductAttributeKeys", t => t.ProductAttributeKeyId)
                .Index(t => t.ProductAttributeKeyId);
            
            CreateTable(
                "dbo.Product_Category_Mapping",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ProductId = c.Int(nullable: false),
                        CategoryId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Categories", t => t.CategoryId, cascadeDelete: true)
                .ForeignKey("dbo.Products", t => t.ProductId, cascadeDelete: true)
                .Index(t => t.ProductId)
                .Index(t => t.CategoryId);
            
            CreateTable(
                "dbo.Categories",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ParentCategoryId = c.Int(nullable: false),
                        Name = c.String(nullable: false, maxLength: 400),
                        Description = c.String(),
                        Published = c.Boolean(nullable: false),
                        Deleted = c.Boolean(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                        CreatedOnUtc = c.DateTime(nullable: false),
                        UpdatedOnUtc = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.ProductVariants",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ProductId = c.Int(nullable: false),
                        ProductAttributeValueId = c.Int(),
                        ItemAttributeKeyId = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Products", t => t.ProductId, cascadeDelete: true)
                .ForeignKey("dbo.ProductAttributeValues", t => t.ProductAttributeValueId)
                .Index(t => t.ProductId)
                .Index(t => t.ProductAttributeValueId);
            
            CreateTable(
                "dbo.ItemAttributeKeys",
                c => new
                    {
                        Id = c.Int(nullable: false),
                        ProductVariantId = c.Int(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                        Name = c.String(),
                        Deleted = c.Boolean(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ProductVariants", t => t.Id)
                .Index(t => t.Id);
            
            CreateTable(
                "dbo.ItemAttributeValues",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ItemAttributeKeyId = c.Int(nullable: false),
                        Value = c.String(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ItemAttributeKeys", t => t.ItemAttributeKeyId, cascadeDelete: true)
                .Index(t => t.ItemAttributeKeyId);
            
            CreateTable(
                "dbo.Items",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ProductVariantId = c.Int(nullable: false),
                        Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                        StockQuantity = c.Int(nullable: false),
                        CreatedOn = c.DateTime(nullable: false),
                        UpdatedOn = c.DateTime(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                        Published = c.Boolean(nullable: false),
                        Deleted = c.Boolean(nullable: false),
                        PictureId = c.Int(),
                        ItemAttributeValue_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ItemAttributeValues", t => t.ItemAttributeValue_Id)
                .ForeignKey("dbo.Pictures", t => t.PictureId)
                .ForeignKey("dbo.ProductVariants", t => t.ProductVariantId, cascadeDelete: true)
                .Index(t => t.ProductVariantId)
                .Index(t => t.PictureId)
                .Index(t => t.ItemAttributeValue_Id);
            
            CreateTable(
                "dbo.Pictures",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        PictureBinary = c.Binary(),
                        MimeType = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Shops",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ShopName = c.String(maxLength: 1000),
                        CustomerId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Customers", t => t.CustomerId, cascadeDelete: true)
                .Index(t => t.CustomerId);
            
            CreateTable(
                "dbo.Orders",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ShopId = c.Int(nullable: false),
                        Deleted = c.Boolean(nullable: false),
                        CustomerId = c.Int(nullable: false),
                        ShippingPrice = c.Decimal(nullable: false, precision: 18, scale: 4),
                        OrderTotal = c.Decimal(nullable: false, precision: 18, scale: 4),
                        RefundedAmount = c.Decimal(nullable: false, precision: 18, scale: 4),
                        PaymentMethodSystemName = c.String(),
                        PaidDate = c.DateTime(),
                        ShippingAddressId = c.Int(),
                        CreatedOn = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Customers", t => t.CustomerId, cascadeDelete: true)
                .ForeignKey("dbo.Addresses", t => t.ShippingAddressId)
                .ForeignKey("dbo.Shops", t => t.ShopId)
                .Index(t => t.ShopId)
                .Index(t => t.CustomerId)
                .Index(t => t.ShippingAddressId);
            
            CreateTable(
                "dbo.Customers",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Username = c.String(maxLength: 1000),
                        UserGuid = c.Guid(nullable: false),
                        Email = c.String(maxLength: 1000),
                        Password = c.String(),
                        NickName = c.String(),
                        Intro = c.String(),
                        SelectedPaymentMethodSystemName = c.String(),
                        ShopId = c.Int(),
                        CustomerLogoId = c.Int(),
                        Deleted = c.Boolean(nullable: false),
                        Active = c.Boolean(nullable: false),
                        CreatedOn = c.DateTime(nullable: false),
                        FriendShipGroupId = c.Int(),
                        ShippingAddress_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.CustomerLogos", t => t.CustomerLogoId)
                .ForeignKey("dbo.Addresses", t => t.ShippingAddress_Id)
                .ForeignKey("dbo.Shops", t => t.ShopId)
                .Index(t => t.ShopId)
                .Index(t => t.CustomerLogoId)
                .Index(t => t.ShippingAddress_Id);
            
            CreateTable(
                "dbo.CustomerLogos",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        PictureId = c.Int(nullable: false),
                        DisplayOrder = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Pictures", t => t.PictureId, cascadeDelete: true)
                .Index(t => t.PictureId);
            
            CreateTable(
                "dbo.CustomerRoles",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Active = c.Boolean(nullable: false),
                        SystemName = c.String(maxLength: 255),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.PermissionRecords",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        SystemName = c.String(nullable: false, maxLength: 255),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.ShoppingCartItems",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        CustomerId = c.Int(nullable: false),
                        Quantity = c.Int(nullable: false),
                        ItemID = c.Int(nullable: false),
                        CreatedOnUtc = c.DateTime(nullable: false),
                        UpdatedOnUtc = c.DateTime(nullable: false),
                        AttributesXml = c.String(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Customers", t => t.CustomerId, cascadeDelete: true)
                .ForeignKey("dbo.Items", t => t.ItemID, cascadeDelete: true)
                .Index(t => t.CustomerId)
                .Index(t => t.ItemID);
            
            CreateTable(
                "dbo.OrderItems",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        OrderId = c.Int(nullable: false),
                        ItemId = c.Int(nullable: false),
                        AttributeDescription = c.String(),
                        Quantity = c.Int(nullable: false),
                        UnitPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                        ItemWeight = c.Decimal(nullable: false, precision: 18, scale: 2),
                        TotalPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Items", t => t.ItemId, cascadeDelete: true)
                .ForeignKey("dbo.Orders", t => t.OrderId, cascadeDelete: true)
                .Index(t => t.OrderId)
                .Index(t => t.ItemId);
            
            CreateTable(
                "dbo.Trading_Order_Mapping",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        OrderId = c.Int(nullable: false),
                        TradingId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Orders", t => t.OrderId, cascadeDelete: true)
                .ForeignKey("dbo.Tradings", t => t.TradingId, cascadeDelete: true)
                .Index(t => t.OrderId)
                .Index(t => t.TradingId);
            
            CreateTable(
                "dbo.Tradings",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Created = c.DateTime(nullable: false),
                        PaymentMethodSystemName = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Messages",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Languageid = c.Int(nullable: false),
                        ResourceName = c.String(),
                        ResourceValue = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Shipments",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        OrderId = c.Int(nullable: false),
                        TrackingNo = c.String(),
                        CreatedOn = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.TradingIds",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.CustomerAddresses",
                c => new
                    {
                        Customer_Id = c.Int(nullable: false),
                        Address_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Customer_Id, t.Address_Id })
                .ForeignKey("dbo.Customers", t => t.Customer_Id, cascadeDelete: true)
                .ForeignKey("dbo.Addresses", t => t.Address_Id, cascadeDelete: true)
                .Index(t => t.Customer_Id)
                .Index(t => t.Address_Id);
            
            CreateTable(
                "dbo.PermissionRecord_Role_Mapping",
                c => new
                    {
                        PermissionRecord_Id = c.Int(nullable: false),
                        CustomerRole_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.PermissionRecord_Id, t.CustomerRole_Id })
                .ForeignKey("dbo.PermissionRecords", t => t.PermissionRecord_Id, cascadeDelete: true)
                .ForeignKey("dbo.CustomerRoles", t => t.CustomerRole_Id, cascadeDelete: true)
                .Index(t => t.PermissionRecord_Id)
                .Index(t => t.CustomerRole_Id);
            
            CreateTable(
                "dbo.Customer_CustomerRole_Mapping",
                c => new
                    {
                        Customer_Id = c.Int(nullable: false),
                        CustomerRole_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Customer_Id, t.CustomerRole_Id })
                .ForeignKey("dbo.Customers", t => t.Customer_Id, cascadeDelete: true)
                .ForeignKey("dbo.CustomerRoles", t => t.CustomerRole_Id, cascadeDelete: true)
                .Index(t => t.Customer_Id)
                .Index(t => t.CustomerRole_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Product_Brand_Mapping", "ProductId", "dbo.Products");
            DropForeignKey("dbo.Products", "ShopId", "dbo.Shops");
            DropForeignKey("dbo.Shops", "CustomerId", "dbo.Customers");
            DropForeignKey("dbo.Trading_Order_Mapping", "TradingId", "dbo.Tradings");
            DropForeignKey("dbo.Trading_Order_Mapping", "OrderId", "dbo.Orders");
            DropForeignKey("dbo.Orders", "ShopId", "dbo.Shops");
            DropForeignKey("dbo.Orders", "ShippingAddressId", "dbo.Addresses");
            DropForeignKey("dbo.OrderItems", "OrderId", "dbo.Orders");
            DropForeignKey("dbo.OrderItems", "ItemId", "dbo.Items");
            DropForeignKey("dbo.Orders", "CustomerId", "dbo.Customers");
            DropForeignKey("dbo.ShoppingCartItems", "ItemID", "dbo.Items");
            DropForeignKey("dbo.ShoppingCartItems", "CustomerId", "dbo.Customers");
            DropForeignKey("dbo.Customers", "ShopId", "dbo.Shops");
            DropForeignKey("dbo.Customers", "ShippingAddress_Id", "dbo.Addresses");
            DropForeignKey("dbo.Customer_CustomerRole_Mapping", "CustomerRole_Id", "dbo.CustomerRoles");
            DropForeignKey("dbo.Customer_CustomerRole_Mapping", "Customer_Id", "dbo.Customers");
            DropForeignKey("dbo.PermissionRecord_Role_Mapping", "CustomerRole_Id", "dbo.CustomerRoles");
            DropForeignKey("dbo.PermissionRecord_Role_Mapping", "PermissionRecord_Id", "dbo.PermissionRecords");
            DropForeignKey("dbo.Customers", "CustomerLogoId", "dbo.CustomerLogos");
            DropForeignKey("dbo.CustomerLogos", "PictureId", "dbo.Pictures");
            DropForeignKey("dbo.CustomerAddresses", "Address_Id", "dbo.Addresses");
            DropForeignKey("dbo.CustomerAddresses", "Customer_Id", "dbo.Customers");
            DropForeignKey("dbo.ProductVariants", "ProductAttributeValueId", "dbo.ProductAttributeValues");
            DropForeignKey("dbo.ProductVariants", "ProductId", "dbo.Products");
            DropForeignKey("dbo.Items", "ProductVariantId", "dbo.ProductVariants");
            DropForeignKey("dbo.Items", "PictureId", "dbo.Pictures");
            DropForeignKey("dbo.Items", "ItemAttributeValue_Id", "dbo.ItemAttributeValues");
            DropForeignKey("dbo.ItemAttributeKeys", "Id", "dbo.ProductVariants");
            DropForeignKey("dbo.ItemAttributeValues", "ItemAttributeKeyId", "dbo.ItemAttributeKeys");
            DropForeignKey("dbo.Product_Category_Mapping", "ProductId", "dbo.Products");
            DropForeignKey("dbo.Product_Category_Mapping", "CategoryId", "dbo.Categories");
            DropForeignKey("dbo.ProductAttributeKeys", "Id", "dbo.Products");
            DropForeignKey("dbo.ProductAttributeValues", "ProductAttributeKeyId", "dbo.ProductAttributeKeys");
            DropForeignKey("dbo.Product_Brand_Mapping", "BrandId", "dbo.Brands");
            DropIndex("dbo.Customer_CustomerRole_Mapping", new[] { "CustomerRole_Id" });
            DropIndex("dbo.Customer_CustomerRole_Mapping", new[] { "Customer_Id" });
            DropIndex("dbo.PermissionRecord_Role_Mapping", new[] { "CustomerRole_Id" });
            DropIndex("dbo.PermissionRecord_Role_Mapping", new[] { "PermissionRecord_Id" });
            DropIndex("dbo.CustomerAddresses", new[] { "Address_Id" });
            DropIndex("dbo.CustomerAddresses", new[] { "Customer_Id" });
            DropIndex("dbo.Trading_Order_Mapping", new[] { "TradingId" });
            DropIndex("dbo.Trading_Order_Mapping", new[] { "OrderId" });
            DropIndex("dbo.OrderItems", new[] { "ItemId" });
            DropIndex("dbo.OrderItems", new[] { "OrderId" });
            DropIndex("dbo.ShoppingCartItems", new[] { "ItemID" });
            DropIndex("dbo.ShoppingCartItems", new[] { "CustomerId" });
            DropIndex("dbo.CustomerLogos", new[] { "PictureId" });
            DropIndex("dbo.Customers", new[] { "ShippingAddress_Id" });
            DropIndex("dbo.Customers", new[] { "CustomerLogoId" });
            DropIndex("dbo.Customers", new[] { "ShopId" });
            DropIndex("dbo.Orders", new[] { "ShippingAddressId" });
            DropIndex("dbo.Orders", new[] { "CustomerId" });
            DropIndex("dbo.Orders", new[] { "ShopId" });
            DropIndex("dbo.Shops", new[] { "CustomerId" });
            DropIndex("dbo.Items", new[] { "ItemAttributeValue_Id" });
            DropIndex("dbo.Items", new[] { "PictureId" });
            DropIndex("dbo.Items", new[] { "ProductVariantId" });
            DropIndex("dbo.ItemAttributeValues", new[] { "ItemAttributeKeyId" });
            DropIndex("dbo.ItemAttributeKeys", new[] { "Id" });
            DropIndex("dbo.ProductVariants", new[] { "ProductAttributeValueId" });
            DropIndex("dbo.ProductVariants", new[] { "ProductId" });
            DropIndex("dbo.Product_Category_Mapping", new[] { "CategoryId" });
            DropIndex("dbo.Product_Category_Mapping", new[] { "ProductId" });
            DropIndex("dbo.ProductAttributeValues", new[] { "ProductAttributeKeyId" });
            DropIndex("dbo.ProductAttributeKeys", new[] { "Id" });
            DropIndex("dbo.Products", new[] { "ShopId" });
            DropIndex("dbo.Product_Brand_Mapping", new[] { "BrandId" });
            DropIndex("dbo.Product_Brand_Mapping", new[] { "ProductId" });
            DropTable("dbo.Customer_CustomerRole_Mapping");
            DropTable("dbo.PermissionRecord_Role_Mapping");
            DropTable("dbo.CustomerAddresses");
            DropTable("dbo.TradingIds");
            DropTable("dbo.Shipments");
            DropTable("dbo.Messages");
            DropTable("dbo.Tradings");
            DropTable("dbo.Trading_Order_Mapping");
            DropTable("dbo.OrderItems");
            DropTable("dbo.ShoppingCartItems");
            DropTable("dbo.PermissionRecords");
            DropTable("dbo.CustomerRoles");
            DropTable("dbo.CustomerLogos");
            DropTable("dbo.Customers");
            DropTable("dbo.Orders");
            DropTable("dbo.Shops");
            DropTable("dbo.Pictures");
            DropTable("dbo.Items");
            DropTable("dbo.ItemAttributeValues");
            DropTable("dbo.ItemAttributeKeys");
            DropTable("dbo.ProductVariants");
            DropTable("dbo.Categories");
            DropTable("dbo.Product_Category_Mapping");
            DropTable("dbo.ProductAttributeValues");
            DropTable("dbo.ProductAttributeKeys");
            DropTable("dbo.Products");
            DropTable("dbo.Product_Brand_Mapping");
            DropTable("dbo.Brands");
            DropTable("dbo.Blogs");
            DropTable("dbo.Addresses");
        }
    }
}
